package com.wang.commons.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${swagger.version}")
    private String version;
    @Value("${swagger.title}")
    private String title;
    @Value("${swagger.description}")
    private String description;
    @Value("${swagger.contact.name}")
    private String name;
    @Value("${swagger.contact.url}")
    private String url;
    @Value("${swagger.contact.email}")
    private String email;
    @Value("${swagger.requestHandlerSelectors.basePackage}")
    private String basePackage;


    @Bean
    public Docket createDocket() {
        //创建文档对象
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        //创建文档的整体说明信息对象
        ApiInfo apiInfo = new ApiInfoBuilder()
                .version(version)
                .title(title)
                .description(description)
                .contact(new Contact(name, url, email))
                .build();
        //把信息对象设置到文档对象上面
        return docket.apiInfo(apiInfo)
                //指定生成文档所需要的接口类扫描路径
                .select()
                //指定扫描包的范围
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                //扫描范围内所有的接口都生成
                .paths(PathSelectors.any())
                .build();
    }
}